<template>
    <view class="my-order">
        <!-- 自定义顶部栏 -->
        <view class="custom-header" :style="{paddingTop: statusBarHeight + 'px'}">
            <view class="left-area" @tap="goBack">
                <image src="/static/images/back.png" mode="aspectFit" class="icon-back"></image>
            </view>
            <view class="center-area">
                <text class="name">我的订单</text>
            </view>
            <view class="right-area"></view>
        </view>
        
        <!-- 订单列表 -->
        <view class="order-container" v-if="myOrderInfo.length">
            <view class="item" v-for="(item, index) in myOrderInfo" :key="index">
                <view class="item-top">
                    <text class="cinema-name">{{item.cinema_name}}</text>
                    <text class="status">已完成</text>
                </view>
                <view class="item-center">
                    <view class="left">
                        <image :src="getPoster(item.poster)" mode="aspectFill" class="poster-image"></image>
                    </view>
                    <view class="right">
                        <view class="movie">
                            <text class="movie-name">{{item.movie_name}}</text>
                            <text class="num">{{item.num}}张</text>
                        </view>
                        <view class="date">
                            <text class="day">{{item.show_date.split('-')[1]+'月'+item.show_date.split('-')[2]+'日'}}</text>
                            <text class="time">{{item.show_time}}</text>
                        </view>
                        <view class="cinema">
                            <text class="hall">{{item.hall_name}}</text>
                            <text class="seat" v-for="(itemSeat, valueSeat) in JSON.parse(item.seat_info)" :key="valueSeat">{{formatSeat(itemSeat)}}</text>
                        </view>
                    </view>
                </view>
                <view class="item-bottom">
                    <view class="price">总价:<text>{{item.price}}元</text></view>
                    <view class="ticket-code">取票码:<text>{{item.phone_code}}</text></view>
                </view>
            </view>
        </view>
        
        <!-- 无订单时显示 -->
        <view class="empty-container" v-else>
            <image src="/static/images/empty.png" mode="aspectFit" class="empty-image"></image>
            <text class="empty-text">暂无订单</text>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            myOrderInfo: [],
            statusBarHeight: 20 // 默认状态栏高度
        }
    },
    onLoad(options) {
        // 获取系统状态栏高度
        const systemInfo = uni.getSystemInfoSync();
        this.statusBarHeight = systemInfo.statusBarHeight;
        
        if (options.UID) {
            this.loadInfo(options.UID);
        }
    },
    methods: {
        goBack() {
            uni.navigateBack();
        },
        loadInfo(userId) {
            uni.showLoading({
                title: '加载中...'
            });
            
            uni.request({
                url: 'http://localhost:9090/findUserOrder',
                method: 'GET',
                data: {
                    userId: userId
                },
                success: (res) => {
                    uni.hideLoading();
                    if (res.data.code === 1) {
                        this.myOrderInfo = res.data.orderInfo;
                        // 按订单日期排序（最新的在前）
                        this.myOrderInfo.sort((a, b) => {
                            return new Date(b.order_date) - new Date(a.order_date);
                        });
                    } else {
                        uni.showToast({
                            title: '获取订单失败',
                            icon: 'none'
                        });
                    }
                },
                fail: (err) => {
                    uni.hideLoading();
                    console.log(err);
                    uni.showToast({
                        title: '网络请求失败',
                        icon: 'none'
                    });
                }
            });
        },
        // 格式化座位
        formatSeat(num) {
            if (num % 10 === 0) {
                return (num / 10) + '排' + 10 + '座';
            } else {
                return parseInt(num / 10) + 1 + '排' + num % 10 + '座';
            }
        },
        getPoster(path) {
            return 'http://localhost:9090' + path;
        }
    }
}
</script>

<style>
.my-order {
    width: 100%;
    min-height: 100vh;
    color: #000;
    background-color: #f1f1f1;
    display: flex;
    flex-direction: column;
}

/* 自定义顶部栏样式 */
.custom-header {
    width: 100%;
    height: 90rpx;
    background-color: #dd2727;
    display: flex;
    align-items: center;
    position: relative;
    z-index: 100;
}

.left-area {
    width: 100rpx;
    height: 90rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    left: 0;
}

.icon-back {
    width: 40rpx;
    height: 40rpx;
}

.center-area {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
}

.name {
    color: #ffffff;
    font-size: 36rpx;
    font-weight: 500;
    text-align: center;
}

.right-area {
    width: 100rpx;
    height: 90rpx;
}

/* 订单列表样式 */
.order-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
    background-color: #f1f1f1;
    padding: 20rpx;
    box-sizing: border-box;
    padding-bottom: env(safe-area-inset-bottom); /* 适配全面屏底部安全区域 */
}

.item {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    padding: 20rpx;
    background-color: #fff;
    margin-bottom: 20rpx;
    border-radius: 12rpx;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
}

.item-top, .item-center, .item-bottom {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 28rpx;
    color: #333;
}

.item-top {
    padding-bottom: 16rpx;
    border-bottom: 1rpx solid #f5f5f5;
}

.cinema-name {
    font-weight: 500;
    color: #333;
    font-size: 30rpx;
}

.status {
    color: #07c160;
    font-size: 26rpx;
    background-color: rgba(7, 193, 96, 0.1);
    padding: 4rpx 12rpx;
    border-radius: 20rpx;
}

.item-center {
    width: 100%;
    justify-content: flex-start;
    padding: 20rpx 0;
}

.left {
    width: 180rpx;
    height: 240rpx;
    margin-right: 20rpx;
    border-radius: 8rpx;
    overflow: hidden;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.poster-image {
    width: 180rpx;
    height: 240rpx;
    border-radius: 8rpx;
}

.right {
    flex: 1;
    align-self: flex-start;
    padding-top: 6rpx;
}

.movie {
    display: flex;
    align-items: center;
    margin-bottom: 20rpx;
}

.movie-name {
    font-weight: bold;
    font-size: 36rpx;
    color: #333;
}

.num {
    margin-left: 20rpx;
    font-size: 32rpx;
    color: #333;
    font-weight: bold;
}

.date, .cinema {
    margin: 10rpx 0;
    font-size: 28rpx;
    color: #666;
}

.day, .time, .hall, .seat {
    margin-right: 20rpx;
    background-color: #f8f8f8;
    padding: 4rpx 12rpx;
    border-radius: 6rpx;
    font-size: 26rpx;
}

.item-bottom {
    margin-top: 16rpx;
    padding-top: 16rpx;
    border-top: 1rpx solid #f5f5f5;
}

.price {
    font-size: 28rpx;
    display: flex;
    align-items: center;
}

.price text {
    color: #dd2727;
    font-weight: bold;
    margin-left: 10rpx;
    font-size: 32rpx;
}

.ticket-code {
    color: #333;
    font-size: 28rpx;
    display: flex;
    align-items: center;
}

.ticket-code text {
    color: #dd2727;
    font-weight: bold;
    margin-left: 10rpx;
    font-size: 32rpx;
}

/* 空状态样式 */
.empty-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding-top: 100rpx;
}

.empty-image {
    width: 200rpx;
    height: 200rpx;
    margin-bottom: 20rpx;
}

.empty-text {
    color: #999;
    font-size: 28rpx;
}
</style>